<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>第二节：SQLAlchemy介绍和基本使用 | Flask进阶课程课件</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html" />
    
    
    <link rel="prev" href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="6.2"
        data-chapter-title="第二节：SQLAlchemy介绍和基本使用"
        data-filepath="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.md"
        data-basepath=".."
        data-revision="Tue Aug 14 2018 17:50:50 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="../chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        第一章：学前准备
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/di-yi-jie-ff1a-xu-ni-huan-jing.html">
            
                
                    <a href="../chapter1/di-yi-jie-ff1a-xu-ni-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        第一节：虚拟环境
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="di-er-zhang-ff1a-ren-shi-web.html">
            
                
                    <a href="../di-er-zhang-ff1a-ren-shi-web.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        第二章：认识web
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        第三章：URL与视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-yi-jie-ff1a-di-yi-ge-flask-cheng-xu.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-yi-jie-ff1a-di-yi-ge-flask-cheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        第一节：Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        第二节：项目配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        第三节：URL与视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        第四节：关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        第四章：模版
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-yi-jie-ff1a-mo-ban-jian-jie.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-yi-jie-ff1a-mo-ban-jian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        第一节：模版简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-er-jie-ff1a-jinja2-mo-ban-gai-shu.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-er-jie-ff1a-jinja2-mo-ban-gai-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        第二节：Jinja2模版概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-san-jie-ff1a-jinja2-guo-lv-qi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-san-jie-ff1a-jinja2-guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        第三节：过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-si-jie-ff1a-jinja2-mo-ban-kong-zhi-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-si-jie-ff1a-jinja2-mo-ban-kong-zhi-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        第四节：控制语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-wu-jie-ff1a-jinja2-mo-ban-ce-shi-qi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-wu-jie-ff1a-jinja2-mo-ban-ce-shi-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        第五节：测试器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-liu-jie-ff1a-hong-he-import-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-liu-jie-ff1a-hong-he-import-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        第六节：宏和import语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-qijie-ff1a-include-he-set-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-qijie-ff1a-include-he-set-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        第七节：include和set语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-ba-jie-ff1a-mo-ban-ji-cheng.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-ba-jie-ff1a-mo-ban-ji-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        第八节：模版继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-jiu-jie-ff1a-zhuan-yi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-jiu-jie-ff1a-zhuan-yi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        第九节：转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-jie-ff1a-shu-ju-lei-xing-he-yun-suan-fu.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-jie-ff1a-shu-ju-lei-xing-he-yun-suan-fu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.10.</b>
                        
                        第十节：数据类型和运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-yi-jie-ff1a-jing-tai-wen-jian-de-pei-zhi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-yi-jie-ff1a-jing-tai-wen-jian-de-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.11.</b>
                        
                        第十一节：静态文件的配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        第五章：视图高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji/di-yi-jie-ff1a-lei-shi-tu.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji/di-yi-jie-ff1a-lei-shi-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        第一节：类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji/di-er-jie-ff1a-lan-tu.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji/di-er-jie-ff1a-lan-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        第二节：蓝图和子域名
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第六章：SQLAlchemy数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        第一节：MySQL数据库的安装
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.2" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        第二节：SQLAlchemy介绍和基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        第三节：SQLAlchemy的ORM（1）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1a-sqlalchemy-de-orm-2.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1a-sqlalchemy-de-orm-2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        第四节：SQLAlchemy的ORM（2）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-wu-jie-ff1a-sqlalchemy-de-orm-3.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-wu-jie-ff1a-sqlalchemy-de-orm-3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        第五节：SQLAlchemy的ORM（3）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        第六节：SQLAlchemy的ORM（4）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        第七节：SQLAlchemy的ORM（5）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        第八节：Flask-SQLAlchemy
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="di-qi-zhang-ff1a-flask-script.html">
            
                
                    <a href="../di-qi-zhang-ff1a-flask-script.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        第七章：Flask-Script
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="di-bazhang-ff1a-alembic-jiao-cheng.html">
            
                
                    <a href="../di-bazhang-ff1a-alembic-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        第八章：alembic教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="di-ba-zhang-ff1a-flask-migrate.html">
            
                
                    <a href="../di-ba-zhang-ff1a-flask-migrate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        第九章：Flask-Migrate
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10" data-path="di-jiu-zhang-ff1a-flask-wtf.html">
            
                
                    <a href="../di-jiu-zhang-ff1a-flask-wtf.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        第十章：Flask-WTF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11" data-path="di-shi-yi-zhang-ff1a-cookie-he-session.html">
            
                
                    <a href="../di-shi-yi-zhang-ff1a-cookie-he-session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        第十一章：cookie和session
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="di-shi-zhang-ff1a-shang-xia-wen.html">
            
                
                    <a href="../di-shi-zhang-ff1a-shang-xia-wen.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        第十二章：Flask上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="13" data-path="di-shi-yi-zhang-ff1a-flask-xin-hao-ji-zhi.html">
            
                
                    <a href="../di-shi-yi-zhang-ff1a-flask-xin-hao-ji-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                        第十三章：Flask信号机制
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14" data-path="di-shi-si-zhang-ff1a-flask.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        第十四章：Flask-Restful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="14.1" data-path="di-shi-si-zhang-ff1a-flask/di-yi-jie-ff1a-restful-api-gui-fan.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask/di-yi-jie-ff1a-restful-api-gui-fan.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.1.</b>
                        
                        第一节：Restful API规范
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14.2" data-path="di-shi-si-zhang-ff1a-flask/di-er-jie-ff1a-flask-restful-cha-jian.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask/di-er-jie-ff1a-flask-restful-cha-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.2.</b>
                        
                        第二节：Flask-Restful插件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="15" data-path="di-shi-er-zhang-ff1a-memcached-jiao-cheng.html">
            
                
                    <a href="../di-shi-er-zhang-ff1a-memcached-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>15.</b>
                        
                        第十五章：memcached教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="16" data-path="di-shi-sizhang-ff1a-redis-jiao-cheng.html">
            
                
                    <a href="../di-shi-sizhang-ff1a-redis-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>16.</b>
                        
                        第十六章：redis教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="17" data-path="di-shi-liu-zhang-ff1a-csrf-gong-ji.html">
            
                
                    <a href="../di-shi-liu-zhang-ff1a-csrf-gong-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>17.</b>
                        
                        第十七章：CSRF攻击
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="18" data-path="di-shi-ba-zhang-ff1a-qi-niu-yun-pei-zhi.html">
            
                
                    <a href="../di-shi-ba-zhang-ff1a-qi-niu-yun-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>18.</b>
                        
                        第十八章：七牛云配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="19" data-path="di-shi-jiu-zhang-ff1a-bu-shu-flask-xiang-mu.html">
            
                
                    <a href="../di-shi-jiu-zhang-ff1a-bu-shu-flask-xiang-mu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>19.</b>
                        
                        第十九章：部署Flask项目
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Flask进阶课程课件</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="sqlalchemy&#x4ECB;&#x7ECD;&#x548C;&#x57FA;&#x672C;&#x4F7F;&#x7528;">SQLAlchemy&#x4ECB;&#x7ECD;&#x548C;&#x57FA;&#x672C;&#x4F7F;&#x7528;</h1>
<p>&#x6570;&#x636E;&#x5E93;&#x662F;&#x4E00;&#x4E2A;&#x7F51;&#x7AD9;&#x7684;&#x57FA;&#x7840;&#x3002;<code>Flask</code>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5F88;&#x591A;&#x79CD;&#x6570;&#x636E;&#x5E93;&#x3002;&#x6BD4;&#x5982;<code>MySQL</code>&#xFF0C;<code>MongoDB</code>,<code>SQLite</code>,<code>PostgreSQL</code>&#x7B49;&#x3002;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x4EE5;<code>MySQL</code>&#x4E3A;&#x4F8B;&#x8FDB;&#x884C;&#x8BB2;&#x89E3;&#x3002;&#x800C;&#x5728;<code>Flask</code>&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x8981;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>ORM</code>&#x6765;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x4F7F;&#x7528;<code>ORM</code>&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x5C06;&#x53D8;&#x5F97;&#x975E;&#x5E38;&#x7B80;&#x5355;&#x3002;</p>
<p>&#x5728;&#x8BB2;&#x89E3;<code>Flask</code>&#x4E2D;&#x7684;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;&#x4E4B;&#x524D;&#xFF0C;&#x5148;&#x786E;&#x4FDD;&#x4F60;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x4E86;&#x4EE5;&#x4E0B;&#x8F6F;&#x4EF6;&#xFF1A;</p>
<ul>
<li><code>mysql</code>&#xFF1A;&#x5982;&#x679C;&#x662F;&#x5728;<code>windows</code>&#x4E0A;&#xFF0C;&#x5230;<a href="http://dev.mysql.com/downloads/windows/" target="_blank">&#x5B98;&#x7F51;</a>&#x4E0B;&#x8F7D;&#x3002;&#x5982;&#x679C;&#x662F;<code>ubuntu</code>&#xFF0C;&#x901A;&#x8FC7;&#x547D;&#x4EE4;<code>sudo apt-get install mysql-server libmysqlclient-dev -yq</code>&#x8FDB;&#x884C;&#x4E0B;&#x8F7D;&#x5B89;&#x88C5;&#x3002;</li>
<li><code>MySQLdb</code>&#xFF1A;<code>MySQLdb</code>&#x662F;&#x7528;<code>Python</code>&#x6765;&#x64CD;&#x4F5C;<code>mysql</code>&#x7684;&#x5305;&#xFF0C;&#x56E0;&#x6B64;&#x901A;&#x8FC7;<code>pip</code>&#x6765;&#x5B89;&#x88C5;&#xFF0C;&#x547D;&#x4EE4;&#x5982;&#x4E0B;&#xFF1A;<code>pip install mysql-python</code>&#x3002;</li>
<li><code>pymysql</code>&#xFF1A;<code>pymysql</code>&#x662F;&#x7528;<code>Python</code>&#x6765;&#x64CD;&#x4F5C;<code>mysql</code>&#x7684;&#x5305;&#xFF0C;&#x56E0;&#x6B64;&#x901A;&#x8FC7;<code>pip</code>&#x6765;&#x5B89;&#x88C5;&#xFF0C;&#x547D;&#x4EE4;&#x5982;&#x4E0B;&#xFF1A;<code>pip3 install pymysql</code>&#x3002;&#x5982;&#x679C;&#x60A8;&#x7528;&#x7684;&#x662F;<code>Python 3</code>&#xFF0C;&#x8BF7;&#x5B89;&#x88C5;<code>pymysql</code>&#x3002;</li>
<li><code>SQLAlchemy</code>&#xFF1A;<code>SQLAlchemy</code>&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x7684;<code>ORM</code>&#x6846;&#x67B6;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x540E;&#x9762;&#x4F1A;&#x7528;&#x5230;&#x3002;&#x5B89;&#x88C5;&#x547D;&#x4EE4;&#x4E3A;&#xFF1A;<code>pip3 install SQLAlchemy</code>&#x3002;</li>
</ul>
<h3 id="&#x901A;&#x8FC7;sqlalchemy&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#xFF1A;">&#x901A;&#x8FC7;<code>SQLAlchemy</code>&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#xFF1A;</h3>
<p>&#x9996;&#x5148;&#x6765;&#x770B;&#x4E00;&#x6BB5;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sqlalchemy <span class="hljs-keyword">import</span> create_engine

<span class="hljs-comment"># &#x6570;&#x636E;&#x5E93;&#x7684;&#x914D;&#x7F6E;&#x53D8;&#x91CF;</span>
HOSTNAME = <span class="hljs-string">&apos;127.0.0.1&apos;</span>
PORT     = <span class="hljs-string">&apos;3306&apos;</span>
DATABASE = <span class="hljs-string">&apos;xt_flask&apos;</span>
USERNAME = <span class="hljs-string">&apos;root&apos;</span>
PASSWORD = <span class="hljs-string">&apos;root&apos;</span>
DB_URI = <span class="hljs-string">&apos;mysql+mysqldb://{}:{}@{}:{}/{}&apos;</span>.format(USERNAME,PASSWORD,HOSTNAME,PORT,DATABASE)

<span class="hljs-comment"># &#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x5F15;&#x64CE;</span>
engine = create_engine(DB_URI)

<span class="hljs-comment">#&#x521B;&#x5EFA;&#x8FDE;&#x63A5;</span>
<span class="hljs-keyword">with</span> engine.connect() <span class="hljs-keyword">as</span> con:
    rs = con.execute(<span class="hljs-string">&apos;SELECT 1&apos;</span>)
    <span class="hljs-keyword">print</span> rs.fetchone()
</code></pre>
<p>&#x9996;&#x5148;&#x4ECE;<code>sqlalchemy</code>&#x4E2D;&#x5BFC;&#x5165;<code>create_engine</code>&#xFF0C;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA;&#x5F15;&#x64CE;&#xFF0C;&#x7136;&#x540E;&#x7528;<code>engine.connect()</code>&#x6765;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x3002;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x6BD4;&#x8F83;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x70B9;&#x662F;&#xFF0C;&#x901A;&#x8FC7;<code>create_engine</code>&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x4F20;&#x9012;&#x4E00;&#x4E2A;&#x6EE1;&#x8DB3;&#x67D0;&#x79CD;&#x683C;&#x5F0F;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5BF9;&#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x683C;&#x5F0F;&#x6765;&#x8FDB;&#x884C;&#x89E3;&#x91CA;&#xFF1A;</p>
<pre><code>dialect+driver://username:password@host:port/database?charset=utf8
</code></pre><p><code>dialect</code>&#x662F;&#x6570;&#x636E;&#x5E93;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x6BD4;&#x5982;<code>MySQL</code>&#x3001;<code>PostgreSQL</code>&#x3001;<code>SQLite</code>&#xFF0C;&#x5E76;&#x4E14;&#x8F6C;&#x6362;&#x6210;&#x5C0F;&#x5199;&#x3002;<code>driver</code>&#x662F;<code>Python</code>&#x5BF9;&#x5E94;&#x7684;&#x9A71;&#x52A8;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x6307;&#x5B9A;&#xFF0C;&#x4F1A;&#x9009;&#x62E9;&#x9ED8;&#x8BA4;&#x7684;&#x9A71;&#x52A8;&#xFF0C;&#x6BD4;&#x5982;MySQL&#x7684;&#x9ED8;&#x8BA4;&#x9A71;&#x52A8;&#x662F;<code>MySQLdb</code>&#x3002;<code>username</code>&#x662F;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x7684;&#x7528;&#x6237;&#x540D;&#xFF0C;<code>password</code>&#x662F;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x7684;&#x5BC6;&#x7801;&#xFF0C;<code>host</code>&#x662F;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x7684;&#x57DF;&#x540D;&#xFF0C;<code>port</code>&#x662F;&#x6570;&#x636E;&#x5E93;&#x76D1;&#x542C;&#x7684;&#x7AEF;&#x53E3;&#x53F7;&#xFF0C;<code>database</code>&#x662F;&#x8FDE;&#x63A5;&#x54EA;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x7684;&#x540D;&#x5B57;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4EE5;&#x4E0A;&#x8F93;&#x51FA;&#x4E86;<code>1</code>&#xFF0C;&#x8BF4;&#x660E;<code>SQLAlchemy</code>&#x80FD;&#x6210;&#x529F;&#x8FDE;&#x63A5;&#x5230;&#x6570;&#x636E;&#x5E93;&#x3002;</p>
<h3 id="&#x7528;sqlalchemy&#x6267;&#x884C;&#x539F;&#x751F;sql&#xFF1A;">&#x7528;SQLAlchemy&#x6267;&#x884C;&#x539F;&#x751F;SQL&#xFF1A;</h3>
<p>&#x6211;&#x4EEC;&#x5C06;&#x4E0A;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x5E93;&#x914D;&#x7F6E;&#x9009;&#x9879;&#x5355;&#x72EC;&#x653E;&#x5728;&#x4E00;&#x4E2A;<code>constants.py</code>&#x7684;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x770B;&#x4EE5;&#x4E0B;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-python">from sqlalchemy import create_engine
from constants import DB_URI

#&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;
engine = create_engine(DB_URI,echo=True)

# &#x4F7F;&#x7528;with&#x8BED;&#x53E5;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x5982;&#x679C;&#x53D1;&#x751F;&#x5F02;&#x5E38;&#x4F1A;&#x88AB;&#x6355;&#x83B7;
with engine.connect() as con:
    # &#x5148;&#x5220;&#x9664;users&#x8868;
    con.execute(&apos;drop table if exists authors&apos;)
    # &#x521B;&#x5EFA;&#x4E00;&#x4E2A;users&#x8868;&#xFF0C;&#x6709;&#x81EA;&#x589E;&#x957F;&#x7684;id&#x548C;name
    con.execute(&apos;create table authors(id int primary key auto_increment,&apos;name varchar(25))&apos;)
    # &#x63D2;&#x5165;&#x4E24;&#x6761;&#x6570;&#x636E;&#x5230;&#x8868;&#x4E2D;
    con.execute(&apos;insert into persons(name) values(&quot;abc&quot;)&apos;)
    con.execute(&apos;insert into persons(name) values(&quot;xiaotuo&quot;)&apos;)
    # &#x6267;&#x884C;&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;
    results = con.execute(&apos;select * from persons&apos;)
    # &#x4ECE;&#x67E5;&#x627E;&#x7684;&#x7ED3;&#x679C;&#x4E2D;&#x904D;&#x5386;
    for result in results:
        print(result)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html" class="navigation navigation-prev " aria-label="Previous page: 第一节：MySQL数据库的安装"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html" class="navigation navigation-next " aria-label="Next page: 第三节：SQLAlchemy的ORM（1）"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
